/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gmovil_jpa.controladores;

import gmovil_jpa.controladores.exceptions.IllegalOrphanException;
import gmovil_jpa.controladores.exceptions.NonexistentEntityException;
import gmovil_jpa.entidades.Atributo;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import gmovil_jpa.entidades.TipoAtributo;
import gmovil_jpa.entidades.ValorAtributoPlan;
import java.util.ArrayList;
import java.util.List;
import gmovil_jpa.entidades.ValorAtributoArticulo;
import gmovil_jpa.entidades.AtributoTipoArticulo;

/**
 *
 * @author castlerock
 */
public class AtributoJpaController {

    public AtributoJpaController() {
        emf = Persistence.createEntityManagerFactory("gmovil_jpaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Atributo atributo) {
        if (atributo.getValorAtributoPlanList() == null) {
            atributo.setValorAtributoPlanList(new ArrayList<ValorAtributoPlan>());
        }
        if (atributo.getValorAtributoArticuloList() == null) {
            atributo.setValorAtributoArticuloList(new ArrayList<ValorAtributoArticulo>());
        }
        if (atributo.getAtributoTipoArticuloList() == null) {
            atributo.setAtributoTipoArticuloList(new ArrayList<AtributoTipoArticulo>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoAtributo tipoAtributo = atributo.getTipoAtributo();
            if (tipoAtributo != null) {
                tipoAtributo = em.getReference(tipoAtributo.getClass(), tipoAtributo.getIdTipoAtributo());
                atributo.setTipoAtributo(tipoAtributo);
            }
            List<ValorAtributoPlan> attachedValorAtributoPlanList = new ArrayList<ValorAtributoPlan>();
            for (ValorAtributoPlan valorAtributoPlanListValorAtributoPlanToAttach : atributo.getValorAtributoPlanList()) {
                valorAtributoPlanListValorAtributoPlanToAttach = em.getReference(valorAtributoPlanListValorAtributoPlanToAttach.getClass(), valorAtributoPlanListValorAtributoPlanToAttach.getIdValorAtributoPlan());
                attachedValorAtributoPlanList.add(valorAtributoPlanListValorAtributoPlanToAttach);
            }
            atributo.setValorAtributoPlanList(attachedValorAtributoPlanList);
            List<ValorAtributoArticulo> attachedValorAtributoArticuloList = new ArrayList<ValorAtributoArticulo>();
            for (ValorAtributoArticulo valorAtributoArticuloListValorAtributoArticuloToAttach : atributo.getValorAtributoArticuloList()) {
                valorAtributoArticuloListValorAtributoArticuloToAttach = em.getReference(valorAtributoArticuloListValorAtributoArticuloToAttach.getClass(), valorAtributoArticuloListValorAtributoArticuloToAttach.getIdValorAtributoArticulo());
                attachedValorAtributoArticuloList.add(valorAtributoArticuloListValorAtributoArticuloToAttach);
            }
            atributo.setValorAtributoArticuloList(attachedValorAtributoArticuloList);
            List<AtributoTipoArticulo> attachedAtributoTipoArticuloList = new ArrayList<AtributoTipoArticulo>();
            for (AtributoTipoArticulo atributoTipoArticuloListAtributoTipoArticuloToAttach : atributo.getAtributoTipoArticuloList()) {
                atributoTipoArticuloListAtributoTipoArticuloToAttach = em.getReference(atributoTipoArticuloListAtributoTipoArticuloToAttach.getClass(), atributoTipoArticuloListAtributoTipoArticuloToAttach.getIdAtributoTipoArticulo());
                attachedAtributoTipoArticuloList.add(atributoTipoArticuloListAtributoTipoArticuloToAttach);
            }
            atributo.setAtributoTipoArticuloList(attachedAtributoTipoArticuloList);
            em.persist(atributo);
            if (tipoAtributo != null) {
                tipoAtributo.getAtributoList().add(atributo);
                tipoAtributo = em.merge(tipoAtributo);
            }
            for (ValorAtributoPlan valorAtributoPlanListValorAtributoPlan : atributo.getValorAtributoPlanList()) {
                Atributo oldAtributoOfValorAtributoPlanListValorAtributoPlan = valorAtributoPlanListValorAtributoPlan.getAtributo();
                valorAtributoPlanListValorAtributoPlan.setAtributo(atributo);
                valorAtributoPlanListValorAtributoPlan = em.merge(valorAtributoPlanListValorAtributoPlan);
                if (oldAtributoOfValorAtributoPlanListValorAtributoPlan != null) {
                    oldAtributoOfValorAtributoPlanListValorAtributoPlan.getValorAtributoPlanList().remove(valorAtributoPlanListValorAtributoPlan);
                    oldAtributoOfValorAtributoPlanListValorAtributoPlan = em.merge(oldAtributoOfValorAtributoPlanListValorAtributoPlan);
                }
            }
            for (ValorAtributoArticulo valorAtributoArticuloListValorAtributoArticulo : atributo.getValorAtributoArticuloList()) {
                Atributo oldAtributoOfValorAtributoArticuloListValorAtributoArticulo = valorAtributoArticuloListValorAtributoArticulo.getAtributo();
                valorAtributoArticuloListValorAtributoArticulo.setAtributo(atributo);
                valorAtributoArticuloListValorAtributoArticulo = em.merge(valorAtributoArticuloListValorAtributoArticulo);
                if (oldAtributoOfValorAtributoArticuloListValorAtributoArticulo != null) {
                    oldAtributoOfValorAtributoArticuloListValorAtributoArticulo.getValorAtributoArticuloList().remove(valorAtributoArticuloListValorAtributoArticulo);
                    oldAtributoOfValorAtributoArticuloListValorAtributoArticulo = em.merge(oldAtributoOfValorAtributoArticuloListValorAtributoArticulo);
                }
            }
            for (AtributoTipoArticulo atributoTipoArticuloListAtributoTipoArticulo : atributo.getAtributoTipoArticuloList()) {
                Atributo oldAtributoOfAtributoTipoArticuloListAtributoTipoArticulo = atributoTipoArticuloListAtributoTipoArticulo.getAtributo();
                atributoTipoArticuloListAtributoTipoArticulo.setAtributo(atributo);
                atributoTipoArticuloListAtributoTipoArticulo = em.merge(atributoTipoArticuloListAtributoTipoArticulo);
                if (oldAtributoOfAtributoTipoArticuloListAtributoTipoArticulo != null) {
                    oldAtributoOfAtributoTipoArticuloListAtributoTipoArticulo.getAtributoTipoArticuloList().remove(atributoTipoArticuloListAtributoTipoArticulo);
                    oldAtributoOfAtributoTipoArticuloListAtributoTipoArticulo = em.merge(oldAtributoOfAtributoTipoArticuloListAtributoTipoArticulo);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Atributo atributo) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Atributo persistentAtributo = em.find(Atributo.class, atributo.getIdAtributo());
            TipoAtributo tipoAtributoOld = persistentAtributo.getTipoAtributo();
            TipoAtributo tipoAtributoNew = atributo.getTipoAtributo();
            List<ValorAtributoPlan> valorAtributoPlanListOld = persistentAtributo.getValorAtributoPlanList();
            List<ValorAtributoPlan> valorAtributoPlanListNew = atributo.getValorAtributoPlanList();
            List<ValorAtributoArticulo> valorAtributoArticuloListOld = persistentAtributo.getValorAtributoArticuloList();
            List<ValorAtributoArticulo> valorAtributoArticuloListNew = atributo.getValorAtributoArticuloList();
            List<AtributoTipoArticulo> atributoTipoArticuloListOld = persistentAtributo.getAtributoTipoArticuloList();
            List<AtributoTipoArticulo> atributoTipoArticuloListNew = atributo.getAtributoTipoArticuloList();
            List<String> illegalOrphanMessages = null;
            for (ValorAtributoPlan valorAtributoPlanListOldValorAtributoPlan : valorAtributoPlanListOld) {
                if (!valorAtributoPlanListNew.contains(valorAtributoPlanListOldValorAtributoPlan)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ValorAtributoPlan " + valorAtributoPlanListOldValorAtributoPlan + " since its atributo field is not nullable.");
                }
            }
            for (ValorAtributoArticulo valorAtributoArticuloListOldValorAtributoArticulo : valorAtributoArticuloListOld) {
                if (!valorAtributoArticuloListNew.contains(valorAtributoArticuloListOldValorAtributoArticulo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ValorAtributoArticulo " + valorAtributoArticuloListOldValorAtributoArticulo + " since its atributo field is not nullable.");
                }
            }
            for (AtributoTipoArticulo atributoTipoArticuloListOldAtributoTipoArticulo : atributoTipoArticuloListOld) {
                if (!atributoTipoArticuloListNew.contains(atributoTipoArticuloListOldAtributoTipoArticulo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain AtributoTipoArticulo " + atributoTipoArticuloListOldAtributoTipoArticulo + " since its atributo field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (tipoAtributoNew != null) {
                tipoAtributoNew = em.getReference(tipoAtributoNew.getClass(), tipoAtributoNew.getIdTipoAtributo());
                atributo.setTipoAtributo(tipoAtributoNew);
            }
            List<ValorAtributoPlan> attachedValorAtributoPlanListNew = new ArrayList<ValorAtributoPlan>();
            for (ValorAtributoPlan valorAtributoPlanListNewValorAtributoPlanToAttach : valorAtributoPlanListNew) {
                valorAtributoPlanListNewValorAtributoPlanToAttach = em.getReference(valorAtributoPlanListNewValorAtributoPlanToAttach.getClass(), valorAtributoPlanListNewValorAtributoPlanToAttach.getIdValorAtributoPlan());
                attachedValorAtributoPlanListNew.add(valorAtributoPlanListNewValorAtributoPlanToAttach);
            }
            valorAtributoPlanListNew = attachedValorAtributoPlanListNew;
            atributo.setValorAtributoPlanList(valorAtributoPlanListNew);
            List<ValorAtributoArticulo> attachedValorAtributoArticuloListNew = new ArrayList<ValorAtributoArticulo>();
            for (ValorAtributoArticulo valorAtributoArticuloListNewValorAtributoArticuloToAttach : valorAtributoArticuloListNew) {
                valorAtributoArticuloListNewValorAtributoArticuloToAttach = em.getReference(valorAtributoArticuloListNewValorAtributoArticuloToAttach.getClass(), valorAtributoArticuloListNewValorAtributoArticuloToAttach.getIdValorAtributoArticulo());
                attachedValorAtributoArticuloListNew.add(valorAtributoArticuloListNewValorAtributoArticuloToAttach);
            }
            valorAtributoArticuloListNew = attachedValorAtributoArticuloListNew;
            atributo.setValorAtributoArticuloList(valorAtributoArticuloListNew);
            List<AtributoTipoArticulo> attachedAtributoTipoArticuloListNew = new ArrayList<AtributoTipoArticulo>();
            for (AtributoTipoArticulo atributoTipoArticuloListNewAtributoTipoArticuloToAttach : atributoTipoArticuloListNew) {
                atributoTipoArticuloListNewAtributoTipoArticuloToAttach = em.getReference(atributoTipoArticuloListNewAtributoTipoArticuloToAttach.getClass(), atributoTipoArticuloListNewAtributoTipoArticuloToAttach.getIdAtributoTipoArticulo());
                attachedAtributoTipoArticuloListNew.add(atributoTipoArticuloListNewAtributoTipoArticuloToAttach);
            }
            atributoTipoArticuloListNew = attachedAtributoTipoArticuloListNew;
            atributo.setAtributoTipoArticuloList(atributoTipoArticuloListNew);
            atributo = em.merge(atributo);
            if (tipoAtributoOld != null && !tipoAtributoOld.equals(tipoAtributoNew)) {
                tipoAtributoOld.getAtributoList().remove(atributo);
                tipoAtributoOld = em.merge(tipoAtributoOld);
            }
            if (tipoAtributoNew != null && !tipoAtributoNew.equals(tipoAtributoOld)) {
                tipoAtributoNew.getAtributoList().add(atributo);
                tipoAtributoNew = em.merge(tipoAtributoNew);
            }
            for (ValorAtributoPlan valorAtributoPlanListNewValorAtributoPlan : valorAtributoPlanListNew) {
                if (!valorAtributoPlanListOld.contains(valorAtributoPlanListNewValorAtributoPlan)) {
                    Atributo oldAtributoOfValorAtributoPlanListNewValorAtributoPlan = valorAtributoPlanListNewValorAtributoPlan.getAtributo();
                    valorAtributoPlanListNewValorAtributoPlan.setAtributo(atributo);
                    valorAtributoPlanListNewValorAtributoPlan = em.merge(valorAtributoPlanListNewValorAtributoPlan);
                    if (oldAtributoOfValorAtributoPlanListNewValorAtributoPlan != null && !oldAtributoOfValorAtributoPlanListNewValorAtributoPlan.equals(atributo)) {
                        oldAtributoOfValorAtributoPlanListNewValorAtributoPlan.getValorAtributoPlanList().remove(valorAtributoPlanListNewValorAtributoPlan);
                        oldAtributoOfValorAtributoPlanListNewValorAtributoPlan = em.merge(oldAtributoOfValorAtributoPlanListNewValorAtributoPlan);
                    }
                }
            }
            for (ValorAtributoArticulo valorAtributoArticuloListNewValorAtributoArticulo : valorAtributoArticuloListNew) {
                if (!valorAtributoArticuloListOld.contains(valorAtributoArticuloListNewValorAtributoArticulo)) {
                    Atributo oldAtributoOfValorAtributoArticuloListNewValorAtributoArticulo = valorAtributoArticuloListNewValorAtributoArticulo.getAtributo();
                    valorAtributoArticuloListNewValorAtributoArticulo.setAtributo(atributo);
                    valorAtributoArticuloListNewValorAtributoArticulo = em.merge(valorAtributoArticuloListNewValorAtributoArticulo);
                    if (oldAtributoOfValorAtributoArticuloListNewValorAtributoArticulo != null && !oldAtributoOfValorAtributoArticuloListNewValorAtributoArticulo.equals(atributo)) {
                        oldAtributoOfValorAtributoArticuloListNewValorAtributoArticulo.getValorAtributoArticuloList().remove(valorAtributoArticuloListNewValorAtributoArticulo);
                        oldAtributoOfValorAtributoArticuloListNewValorAtributoArticulo = em.merge(oldAtributoOfValorAtributoArticuloListNewValorAtributoArticulo);
                    }
                }
            }
            for (AtributoTipoArticulo atributoTipoArticuloListNewAtributoTipoArticulo : atributoTipoArticuloListNew) {
                if (!atributoTipoArticuloListOld.contains(atributoTipoArticuloListNewAtributoTipoArticulo)) {
                    Atributo oldAtributoOfAtributoTipoArticuloListNewAtributoTipoArticulo = atributoTipoArticuloListNewAtributoTipoArticulo.getAtributo();
                    atributoTipoArticuloListNewAtributoTipoArticulo.setAtributo(atributo);
                    atributoTipoArticuloListNewAtributoTipoArticulo = em.merge(atributoTipoArticuloListNewAtributoTipoArticulo);
                    if (oldAtributoOfAtributoTipoArticuloListNewAtributoTipoArticulo != null && !oldAtributoOfAtributoTipoArticuloListNewAtributoTipoArticulo.equals(atributo)) {
                        oldAtributoOfAtributoTipoArticuloListNewAtributoTipoArticulo.getAtributoTipoArticuloList().remove(atributoTipoArticuloListNewAtributoTipoArticulo);
                        oldAtributoOfAtributoTipoArticuloListNewAtributoTipoArticulo = em.merge(oldAtributoOfAtributoTipoArticuloListNewAtributoTipoArticulo);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = atributo.getIdAtributo();
                if (findAtributo(id) == null) {
                    throw new NonexistentEntityException("The atributo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Atributo atributo;
            try {
                atributo = em.getReference(Atributo.class, id);
                atributo.getIdAtributo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The atributo with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<ValorAtributoPlan> valorAtributoPlanListOrphanCheck = atributo.getValorAtributoPlanList();
            for (ValorAtributoPlan valorAtributoPlanListOrphanCheckValorAtributoPlan : valorAtributoPlanListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Atributo (" + atributo + ") cannot be destroyed since the ValorAtributoPlan " + valorAtributoPlanListOrphanCheckValorAtributoPlan + " in its valorAtributoPlanList field has a non-nullable atributo field.");
            }
            List<ValorAtributoArticulo> valorAtributoArticuloListOrphanCheck = atributo.getValorAtributoArticuloList();
            for (ValorAtributoArticulo valorAtributoArticuloListOrphanCheckValorAtributoArticulo : valorAtributoArticuloListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Atributo (" + atributo + ") cannot be destroyed since the ValorAtributoArticulo " + valorAtributoArticuloListOrphanCheckValorAtributoArticulo + " in its valorAtributoArticuloList field has a non-nullable atributo field.");
            }
            List<AtributoTipoArticulo> atributoTipoArticuloListOrphanCheck = atributo.getAtributoTipoArticuloList();
            for (AtributoTipoArticulo atributoTipoArticuloListOrphanCheckAtributoTipoArticulo : atributoTipoArticuloListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Atributo (" + atributo + ") cannot be destroyed since the AtributoTipoArticulo " + atributoTipoArticuloListOrphanCheckAtributoTipoArticulo + " in its atributoTipoArticuloList field has a non-nullable atributo field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            TipoAtributo tipoAtributo = atributo.getTipoAtributo();
            if (tipoAtributo != null) {
                tipoAtributo.getAtributoList().remove(atributo);
                tipoAtributo = em.merge(tipoAtributo);
            }
            em.remove(atributo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Atributo> findAtributoEntities() {
        return findAtributoEntities(true, -1, -1);
    }

    public List<Atributo> findAtributoEntities(int maxResults, int firstResult) {
        return findAtributoEntities(false, maxResults, firstResult);
    }

    private List<Atributo> findAtributoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Atributo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Atributo findAtributo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Atributo.class, id);
        } finally {
            em.close();
        }
    }

    public int getAtributoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Atributo> rt = cq.from(Atributo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
